package com.telaxo.shooter2012.foes {
	import com.telaxo.shooter2012.gfx.Ship;
	import com.telaxo.shooter2012.interfaces.Vaisseau;
	import com.telaxo.shooter2012.projectiles.BalleEnnemi;

	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;

	/**
	 * @author Benjamin
	 */
	public class Ennemi1 extends Sprite implements Vaisseau {
		// Récupération de l'image du vaisseau
		[Embed(source="../gfx/swf/Stinger.swf")]
		private var Vivant : Class;
		[Embed(source="../gfx/swf/StingerDetruit.swf")]
		private var Detruit : Class;
		private var stageRef : Stage;
		private var vy : Number = 3;
		private var ay : Number = .00000001;
		private var target : Ship;
		private var image : MovieClip;
		private var hitBox : Sprite;
		private var _state : Number;
		public static var VIVANT : Number = 1;
		public static var DETRUIT : Number = 2;
		public static var DESTRUCTION_COMPLETE : Number = 3;

		public function Ennemi1(s : Stage, t : Ship) {
			image = new MovieClip();
			// Ajout de l'image du vaisseau
			loadSWF("/src/com/telaxo/shooter2012/gfx/swf/Stinger.swf");

			stageRef = s;
			target = t;
			this.x = Math.random() * stageRef.stageWidth;
			this.y = -5;

			// Ajout de la hitbox
			hitBox = new Sprite();
			hitBox.graphics.drawRect(-11, -2, 22, 15);
			hitArea = hitBox;

			// Etats du vaiseau
			state = VIVANT;

			addChild(image);
			addChild(hitBox);

			// Evénement
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
		}

		private function loop(pEvt : Event) : void {
			// Gestion de l'animation du vaisseau ennemi
			switch(_state) {
				case VIVANT:
					vy += ay;
					y += vy;
					// Si le vaisseau sort de l'écran, le supprimer
					if (y > stageRef.stageHeight) supprimerVaisseau();
					// Comportement du vaisseau
					if (y - 15 < target.y && y + 15 > target.y) tirerLaser();
					break;
				case DETRUIT:
					loadSWF("C:/workspace/tutoriel_shooter2012/src/com/telaxo/shooter2012/gfx/swf/StingerDetruit.swf");
					_state = DESTRUCTION_COMPLETE;
					break;
				case DESTRUCTION_COMPLETE:
					trace("CURRENT FRAME : " + image.currentFrame);
					trace("TOTAL FRAME : " + image.totalFrames);
					if (image.currentFrame == image.totalFrames) supprimerVaisseau();
					break;
			}
		}

		public function estTouche() : void {
			_state = DETRUIT;
			rotation = Math.random() * 360;
		}

		public function tirerLaser() : void {
			stageRef.addChild(new BalleEnnemi(stageRef, target, x, y, -5));
			stageRef.addChild(new BalleEnnemi(stageRef, target, x, y, 5));
		}

		public function supprimerVaisseau() : void {
			removeEventListener(Event.ENTER_FRAME, loop);

			if (stageRef.contains(this)) stageRef.removeChild(this);
		}

		public function set state(val : Number) : void {
			_state = val;
		}

		// ACCESSEURS
		public function get hit() : Sprite {
			return hitBox;
		}

		// Charge le swf correspondant au vaisseau ennemi
		private function loadSWF(swfURL : String) : void {
			var myLoader : Loader = new Loader();
			var mySWF : URLRequest = new URLRequest(swfURL);
			myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
			myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
			myLoader.load(mySWF);
		}

		private function onCompleteHandler(loadEvent : Event) : void {
			if (this.contains(image)) removeChild(image);
			image = loadEvent.currentTarget.content;
			addChild(image);
			image.play();
		}

		private function onProgressHandler(myProgress : ProgressEvent) : void {
			var percent : Number = Math.round(myProgress.bytesLoaded / myProgress.bytesTotal * 100);
		}
	}
}
